package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;


import server.SQLConnection;

/**
 * this class implements ordering a lended book
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */

public class MessageOrderBook {

	PreparedStatement ps;
	int rows,temp;
	/**
	 * constractor of this class 
	 * 
	 */

	public MessageOrderBook() {
		super();
	}
	/**
	 * order book and changing it in the DB 
	 * @param userid
	 * @param copyid
	 * @throws SQLException
	 * @throws IOException
	 */
	public ArrayList<String> MakeOrder(String userid, String copyid) throws SQLException, IOException
	{
		//System.out.println("The user id is:  "+userid);
		//System.out.println("The copy id is:  "+copyid);
		Statement stmt;
		SQLConnection conn = new  SQLConnection();
		stmt = conn.getConnection().createStatement();
		ResultSet rs= stmt.executeQuery("SELECT * FROM db1.reservation where db1.reservation.BookID='"+copyid+"'");
		if (!rs.next())
		{
			//System.out.println("Result111111==:  ");
			ResultSet rs1= stmt.executeQuery("SELECT db1.lending.CopyID FROM db1.lending where db1.lending.CopyID='"+copyid+"' AND db1.lending.SubscriberID='"+userid+"' AND  db1.lending.lend='yes'");
			if(rs1.next())
			{
				ArrayList<String> arr=new ArrayList<String>();
				arr.add("OrderBook");
				arr.add("0");
				return (arr);
				}
			ResultSet rs2= stmt.executeQuery("SELECT db1.lending.CopyID FROM db1.lending where db1.lending.CopyID='"+copyid+"' AND db1.lending.SubscriberID='"+userid+"' AND  db1.lending.lend='yes'");
			//System.out.println("ResultAAA==:  "+copyid);
			//System.out.println("ResultBBB==:  "+userid);
			if (!rs2.next())
			{
				
			    java.util.Date today = new java.util.Date();
				java.sql.Date timestamp = new java.sql.Date(today.getTime());
				java.sql.PreparedStatement ps = conn.getConnection().prepareStatement("INSERT INTO  db1.reservation (RID,BookID,ReservationDate,Subscriberid) VALUES (?,?,?,?)");
				ResultSet rs3= stmt.executeQuery("SELECT * FROM db1.reservation;");
				rows=1;
				while(rs3.next()){
					temp=rs3.getInt(1);
						if(rows>=temp)
							rows=rows+1;
							else{
								rows=temp;
							}
				}
				//System.out.println("Result222222==:  "+rows);
				ps.setInt(1,rows);
				ps.setString(2,copyid);
				ps.setDate(3,timestamp);
				ps.setString(4,userid);
				ps.executeUpdate();
				//System.out.println("Result222222==:  ");
				ArrayList<String> arr1=new ArrayList<String>();
				arr1.add("OrderBook");
				arr1.add("1");
				return (arr1);
			}
		}
		else 
			{
				ArrayList<String> arr=new ArrayList<String>();
				arr.add("OrderBook");
				arr.add("0");
				return (arr);
			}
		ArrayList<String> failarr=new ArrayList<String>();
		failarr.add("fail");
		return failarr;
	}
}
